﻿
namespace RIA.ServicesLibrary.Web.Services
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Data;
    using System.Linq;
    using System.ServiceModel.DomainServices.EntityFramework;
    using System.ServiceModel.DomainServices.Hosting;
    using System.ServiceModel.DomainServices.Server;
    using RIA.ServicesLibrary.Web.Models;


    // Implements application logic using the AdventureWorksLT_DataEntities context.
    // TODO: Add your application logic to these methods or in additional methods.
    // TODO: Wire up authentication (Windows/ASP.NET Forms) and uncomment the following to disable anonymous access
    // Also consider adding roles to restrict access as appropriate.
    // [RequiresAuthentication]
    [EnableClientAccess ()]
    public class ProductService : LinqToEntitiesDomainService<AdventureWorksLT_DataEntities>
    {
        public IQueryable<ProductSummary> GetProductSummaryList ()
        {
            return from p in this.ObjectContext.Products
                   orderby p.Name
                   select new ProductSummary ()
                   {
                       ID = p.ProductID,
                       Name = p.Name,
                       Number = p.ProductNumber,
                       ListPrice = p.ListPrice,
                       ThumbnailPhoto =p.ThumbNailPhoto,
                       //QuantityAvailable = p.ProductInventories.Sum ( pi => pi.Quantity ),
                       Category = p.ProductCategory.ParentProductCategory.Name,
                       Subcategory = p.ProductCategory.Name,
                       Model = p.ProductModel.Name,
                      
                   };
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'Products' query.
        public IQueryable<Product> GetProducts ()
        {
            return this.ObjectContext.Products;
        }

        public void InsertProduct ( Product product )
        {
            if ( ( product.EntityState != EntityState.Detached ) )
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState ( product, EntityState.Added );
            }
            else
            {
                this.ObjectContext.Products.AddObject ( product );
            }
        }

        public void UpdateProduct ( Product currentProduct )
        {
            this.ObjectContext.Products.AttachAsModified ( currentProduct, this.ChangeSet.GetOriginal ( currentProduct ) );
        }

        public void DeleteProduct ( Product product )
        {
            if ( ( product.EntityState == EntityState.Detached ) )
            {
                this.ObjectContext.Products.Attach ( product );
            }
            this.ObjectContext.Products.DeleteObject ( product );
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'ProductCategories' query.
        public IQueryable<ProductCategory> GetProductCategories ()
        {
            return this.ObjectContext.ProductCategories;
        }

        public void InsertProductCategory ( ProductCategory productCategory )
        {
            if ( ( productCategory.EntityState != EntityState.Detached ) )
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState ( productCategory, EntityState.Added );
            }
            else
            {
                this.ObjectContext.ProductCategories.AddObject ( productCategory );
            }
        }

        public void UpdateProductCategory ( ProductCategory currentProductCategory )
        {
            this.ObjectContext.ProductCategories.AttachAsModified ( currentProductCategory, this.ChangeSet.GetOriginal ( currentProductCategory ) );
        }

        public void DeleteProductCategory ( ProductCategory productCategory )
        {
            if ( ( productCategory.EntityState == EntityState.Detached ) )
            {
                this.ObjectContext.ProductCategories.Attach ( productCategory );
            }
            this.ObjectContext.ProductCategories.DeleteObject ( productCategory );
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'ProductDescriptions' query.
        public IQueryable<ProductDescription> GetProductDescriptions ()
        {
            return this.ObjectContext.ProductDescriptions;
        }

        public void InsertProductDescription ( ProductDescription productDescription )
        {
            if ( ( productDescription.EntityState != EntityState.Detached ) )
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState ( productDescription, EntityState.Added );
            }
            else
            {
                this.ObjectContext.ProductDescriptions.AddObject ( productDescription );
            }
        }

        public void UpdateProductDescription ( ProductDescription currentProductDescription )
        {
            this.ObjectContext.ProductDescriptions.AttachAsModified ( currentProductDescription, this.ChangeSet.GetOriginal ( currentProductDescription ) );
        }

        public void DeleteProductDescription ( ProductDescription productDescription )
        {
            if ( ( productDescription.EntityState == EntityState.Detached ) )
            {
                this.ObjectContext.ProductDescriptions.Attach ( productDescription );
            }
            this.ObjectContext.ProductDescriptions.DeleteObject ( productDescription );
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'ProductModels' query.
        public IQueryable<ProductModel> GetProductModels ()
        {
            return this.ObjectContext.ProductModels;
        }

        public void InsertProductModel ( ProductModel productModel )
        {
            if ( ( productModel.EntityState != EntityState.Detached ) )
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState ( productModel, EntityState.Added );
            }
            else
            {
                this.ObjectContext.ProductModels.AddObject ( productModel );
            }
        }

        public void UpdateProductModel ( ProductModel currentProductModel )
        {
            this.ObjectContext.ProductModels.AttachAsModified ( currentProductModel, this.ChangeSet.GetOriginal ( currentProductModel ) );
        }

        public void DeleteProductModel ( ProductModel productModel )
        {
            if ( ( productModel.EntityState == EntityState.Detached ) )
            {
                this.ObjectContext.ProductModels.Attach ( productModel );
            }
            this.ObjectContext.ProductModels.DeleteObject ( productModel );
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'ProductModelProductDescriptions' query.
        public IQueryable<ProductModelProductDescription> GetProductModelProductDescriptions ()
        {
            return this.ObjectContext.ProductModelProductDescriptions;
        }

        public void InsertProductModelProductDescription ( ProductModelProductDescription productModelProductDescription )
        {
            if ( ( productModelProductDescription.EntityState != EntityState.Detached ) )
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState ( productModelProductDescription, EntityState.Added );
            }
            else
            {
                this.ObjectContext.ProductModelProductDescriptions.AddObject ( productModelProductDescription );
            }
        }

        public void UpdateProductModelProductDescription ( ProductModelProductDescription currentProductModelProductDescription )
        {
            this.ObjectContext.ProductModelProductDescriptions.AttachAsModified ( currentProductModelProductDescription, this.ChangeSet.GetOriginal ( currentProductModelProductDescription ) );
        }

        public void DeleteProductModelProductDescription ( ProductModelProductDescription productModelProductDescription )
        {
            if ( ( productModelProductDescription.EntityState == EntityState.Detached ) )
            {
                this.ObjectContext.ProductModelProductDescriptions.Attach ( productModelProductDescription );
            }
            this.ObjectContext.ProductModelProductDescriptions.DeleteObject ( productModelProductDescription );
        }
    }
}


